package com.itheima.ui;

import cn.hutool.core.io.FileUtil;
import com.itheima.domain.GameInfo;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    JMenu functionJMenu = new JMenu("功能");
    JMenu aboutJMenu = new JMenu("关于我们");

    JMenu changeImage = new JMenu("更换图片");
    JMenu saveJMenu = new JMenu("存档");
    JMenu loadJMenu = new JMenu("读档");

    JMenuItem girl = new JMenuItem("美女");
    JMenuItem animal = new JMenuItem("动物");
    JMenuItem sport = new JMenuItem("运动");
    JMenuItem accountItem = new JMenuItem("官方");
    JMenuItem payItem = new JMenuItem("充值");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reLoginItem = new JMenuItem("重新登录");

    JMenuItem saveItem0 = new JMenuItem("存档0(空)");
    JMenuItem saveItem1 = new JMenuItem("存档1(空)");
    JMenuItem saveItem2 = new JMenuItem("存档2(空)");
    JMenuItem saveItem3 = new JMenuItem("存档3(空)");
    JMenuItem saveItem4 = new JMenuItem("存档4(空)");

    JMenuItem loadItem0 = new JMenuItem("读档0(空)");
    JMenuItem loadItem1 = new JMenuItem("读档1(空)");
    JMenuItem loadItem2 = new JMenuItem("读档2(空)");
    JMenuItem loadItem3 = new JMenuItem("读档3(空)");
    JMenuItem loadItem4 = new JMenuItem("读档4(空)");

    int[][] data = new int[4][4];
    int[][] win = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };

    int x = 0;
    int y = 0;

    int step = 0;

    String path = "image\\girl\\girl7\\";

    public GameJFrame() throws IOException, ClassNotFoundException {

        initJFrame();

        initJMenuBar();

        initData();

        initImage();

        setVisible(true);
    }

    private void initImage() {

        this.getContentPane().removeAll();

        if (victory()) {
            JLabel jLabel = new JLabel(new ImageIcon("image\\win.png"));
            jLabel.setBounds(203, 283, 197, 73);
            this.getContentPane().add(jLabel);
        }

        JLabel stepCount = new JLabel("步数：" + step);
        stepCount.setBounds(50, 30, 100, 20);
        this.getContentPane().add(stepCount);

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int number = data[i][j];
                JLabel jLabel = new JLabel(new ImageIcon(path + number + ".jpg"));
                jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                this.getContentPane().add(jLabel);
            }
        }
        JLabel background = new JLabel(new ImageIcon("image\\background.png"));
        background.setBounds(40, 40, 508, 560);
        this.getContentPane().add(background);

        this.getContentPane().repaint();
    }

    private boolean victory() {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] != win[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    private void initData() {
        int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        Random r = new Random();
        for (int i = 0; i < tempArr.length; i++) {
            int j = r.nextInt(tempArr.length);
            int temp = tempArr[i];
            tempArr[i] = tempArr[j];
            tempArr[j] = temp;
        }
        int index = 0;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                data[i][j] = tempArr[index++];
                if (data[i][j] == 0) {
                    this.x = i;
                    this.y = j;
                }
            }
        }
//        System.out.println(x);
//        System.out.print(y);
//        for (int i = 0; i < data.length; i++) {
//            for (int j = 0; j < data[i].length; j++) {
//                System.out.println(data[i][j]);
//            }
//        }
    }

    private void initJMenuBar() throws IOException, ClassNotFoundException {
        JMenuBar jMenuBar = new JMenuBar();

        functionJMenu.add(closeItem);
        functionJMenu.add(replayItem);
        functionJMenu.add(reLoginItem);
        functionJMenu.add(changeImage);
        //functionJMenu.add(saveJMenu);
        //functionJMenu.add(loadJMenu);

        aboutJMenu.add(accountItem);
        aboutJMenu.add(payItem);

        saveJMenu.add(saveItem0);
        saveJMenu.add(saveItem1);
        saveJMenu.add(saveItem2);
        saveJMenu.add(saveItem3);
        saveJMenu.add(saveItem4);

        loadJMenu.add(loadItem0);
        loadJMenu.add(loadItem1);
        loadJMenu.add(loadItem2);
        loadJMenu.add(loadItem3);
        loadJMenu.add(loadItem4);

        changeImage.add(girl);
        changeImage.add(animal);
        changeImage.add(sport);

        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);

        closeItem.addActionListener(this);
        replayItem.addActionListener(this);
        reLoginItem.addActionListener(this);
        animal.addActionListener(this);
        girl.addActionListener(this);
        sport.addActionListener(this);
        accountItem.addActionListener(this);
        payItem.addActionListener(this);
        saveItem0.addActionListener(this);
        saveItem1.addActionListener(this);
        saveItem2.addActionListener(this);
        saveItem3.addActionListener(this);
        saveItem4.addActionListener(this);
        loadItem0.addActionListener(this);
        loadItem1.addActionListener(this);
        loadItem2.addActionListener(this);
        loadItem3.addActionListener(this);
        loadItem4.addActionListener(this);

       // getGameInfo();

        this.setJMenuBar(jMenuBar);

    }

    private void getGameInfo() throws IOException, ClassNotFoundException {
        File file=new File("mypuzzle\\save");
        File[] files = file.listFiles();
        for (File f : files) {
            ObjectInputStream ois=new ObjectInputStream(new FileInputStream(f));
            GameInfo gi = (GameInfo)ois.readObject();
            ois.close();
            int step=gi.getStep();
            String name=f.getName();
            //获取当存档的序号（索引）
            int index = name.charAt(4) - '0';
            //修改菜单上所表示的文字信息
            saveJMenu.getItem(index).setText("存档" + index + "(" + step + ")步");
            loadJMenu.getItem(index).setText("存档" + index + "(" + step + ")步");
        }


    }

    private void initJFrame() {
        this.setSize(603, 680);
        this.setTitle("拼图单机版 v1.0");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);

        this.addKeyListener(this);
    }
    private void showJDialog(String content) {
        JDialog jDialog=new JDialog();
        jDialog.setSize(200,150);
        jDialog.setAlwaysOnTop(true);
        jDialog.setLocationRelativeTo(null);
        jDialog.setModal(true);
        jDialog.setVisible(true);
        JLabel jLabel=new JLabel(content);
        jLabel.setBounds(0,0,200,150);
        jDialog.getContentPane().add(jLabel);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if (obj == closeItem) {
            System.out.println("关闭游戏");
            System.exit(0);
        } else if (obj == accountItem) {
            System.out.println("点击了官方微信二维码");
            JDialog jDialog = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon("image\\wechat.png"));
            jLabel.setBounds(0, 0, 346, 346);
            jDialog.getContentPane().add(jLabel);
            jDialog.setTitle("游戏负责人");
            jDialog.setSize(400, 400);
            jDialog.setLocationRelativeTo(null);
            jDialog.setAlwaysOnTop(true);
            jDialog.setModal(true);
            jDialog.setVisible(true);
        } else if (obj == payItem) {
            System.out.println("点击了充值服务");
            JDialog jDialog = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon("image\\pay.png"));
            jLabel.setBounds(0, 0, 346, 346);
            jDialog.getContentPane().add(jLabel);
            jDialog.setTitle("充值1.66后添加游戏负责人查询作弊码");
            jDialog.setSize(450, 450);
            jDialog.setLocationRelativeTo(null);
            jDialog.setAlwaysOnTop(true);
            jDialog.setModal(true);
            jDialog.setVisible(true);
        } else if (obj == replayItem) {
            System.out.println("点击了重新游戏");
            step = 0;
            initData();
            initImage();
        } else if (obj == girl) {
            changeImage(obj);
        } else if (obj == animal) {
            changeImage(obj);
        } else if (obj == sport) {
            changeImage(obj);
        }else if(obj==reLoginItem){
            System.out.println("点击了重新登陆");
            this.setVisible(false);
            new LoginJFrame();
        }

    }

    private void changeImage(Object obj) {

        Random r = new Random();
        int num = 0;
        if (obj == girl) {
            System.out.println("点击了" + girl);
            num = r.nextInt(13) + 1;
            path = "image\\girl\\girl" + num + "\\";
        } else if (obj == animal) {
            System.out.println("点击了" + animal);
            num = r.nextInt(8) + 1;
            path = "image\\animal\\animal" + num + "\\";
        } else if (obj == sport) {
            System.out.println("点击了" + sport);
            num = r.nextInt(10) + 1;
            path = "image\\sport\\sport" + num + "\\";
        }

        step = 0;
        initData();
        initImage();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        //System.out.println(e);
        if (key == 37) {
            System.out.println("向左移动");
            if (y == 3) {
                return;
            }
            data[x][y] = data[x][y + 1];
            data[x][y + 1] = 0;
            y++;
            step++;
            initImage();
        } else if (key == 38) {
            System.out.println("向上移动");
            if (x == 3) {
                return;
            }
            data[x][y] = data[x + 1][y];
            data[x + 1][y] = 0;
            x++;
            step++;
            initImage();
        } else if (key == 39) {
            System.out.println("向右移动");
            if (y == 0) {
                return;
            }
            data[x][y] = data[x][y - 1];
            data[x][y - 1] = 0;
            y--;
            step++;
            initImage();
        } else if (key == 40) {
            System.out.println("向下移动");
            if (x == 0) {
                return;
            }
            data[x][y] = data[x - 1][y];
            data[x - 1][y] = 0;
            x--;
            step++;
            initImage();
        } else if (key == 87) {
            System.out.println("按下了w");
            int[][] tempArr = {
                    {1, 2, 3, 4},
                    {5, 6, 7, 8},
                    {9, 10, 11, 12},
                    {13, 14, 15, 0}
            };
            data = tempArr;
            initImage();
        }
    }
}
